#include "stdafx.h"
#include "ResourceManager.h"
#include "ShaderManager.h"
#include "TextureManager.h"
#include "ModelManager.h"
#include "RenderSystem.h"
#include "AudioManager.h"
#include <fstream>

ResourceManager::ResourceManager(RenderSystem* render_system)
{
	// parse the resources.txt file for all paths
	std::ifstream file("../data/resources.txt");
	char input;
	String shader_directory;
	String texture_directory;
	String model_directory;
	String audio_directory;

	file.get(input);

	while (!file.eof())
	{
		if (input == '#')
		{
			while (input != '\n')
				file.get(input);

			file.get(input);
		}
		else if (input == 's')
		{
			file >> shader_directory;
			file.get(input);
		}
		else if (input == 't')
		{
			file >> texture_directory;
			file.get(input);
		}
		else if (input == 'm')
		{
			file >> model_directory;
			file.get(input);
		}
		else if (input == 'a')
		{
			file >> audio_directory;
			file.get(input);
		}
		else
			file.get(input);
	}

	file.close();

	m_shader_manager = new ShaderManager(render_system, shader_directory);
	m_texture_manager = new TextureManager(render_system, texture_directory);
	m_model_manager = new ModelManager(render_system, model_directory);
	m_audio_manager = new AudioManager(audio_directory);
}

ResourceManager::~ResourceManager(void)
{
	if (m_shader_manager)
		delete m_shader_manager;

	if (m_texture_manager)
		delete m_texture_manager;

	if (m_model_manager)
		delete m_model_manager;
	if (m_audio_manager)
		delete m_audio_manager;
}

Shader* ResourceManager::GetShader(const String& vs_filename, const String& ps_filename)
{
	return m_shader_manager->Create(vs_filename, ps_filename);
}

Texture* ResourceManager::GetTexture(const String& texture_filename)
{
	return m_texture_manager->Create(texture_filename);
}

Renderable* ResourceManager::GetModel(const String& model_filename)
{
	return m_model_manager->Create(model_filename);
}

Sound2D* ResourceManager::GetSound2D(const String& filename){
	return m_audio_manager->Load2DSound(filename);
}

Sound3D* ResourceManager::GetSound3D(const String& filename){
	return m_audio_manager->Load3DSound(filename);
}

void ResourceManager::Remove2DSound(const String& filename){
	m_audio_manager->Remove2DSound(filename);
}
void ResourceManager::Remove3DSound(const String& filename){
	m_audio_manager->Remove3DSound(filename);
}